package cn.mandifal.login.service;

import cn.mandifal.manager.service.AdminService;
import cn.mandifal.pojo.DpAdmin;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.ArrayList;
import java.util.List;

public class UserDetailsServiceImpl implements UserDetailsService {
    @Reference
    private AdminService adminService;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //创建管理员角色列表
        List<GrantedAuthority> grantedAuthority=new ArrayList<GrantedAuthority>();
        grantedAuthority.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        DpAdmin adminUsername = adminService.findByUsername(username);
        if (adminUsername !=null){
            return new User(adminUsername.getUsername(),adminUsername.getPassword(),grantedAuthority);
        }else{
            return null;
        }

    }
}
